package com.nuanshui.framework.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;


public class GenerateRedPackets {

	
	 public static  List<Double> divide(double totalMoney,int num,double minMoney){
		   //如果最小保护值与生成的红包个数和大于总数 则最小值为平均值
		   if(minMoney*num>totalMoney) minMoney = totalMoney/num;
		   //如果最小红包值小于一分，则最小值为一分
		   if(minMoney<0.01) minMoney = 0.01;
	        List<Double> each = new ArrayList<Double>(num);
	        NumberFormat nf = new DecimalFormat("#.##");
	        for(int i=0;i<num-1;i++){
	        	//获取最大可以生成的红包额
	            double maxSafeMoney = (totalMoney - (num-1-i)*minMoney);
	            //如果可以生成红包的最大值大于剩余总金额的1/3 则最大值为剩余总金额的1/3
	            if(totalMoney/3<maxSafeMoney) maxSafeMoney = totalMoney/3;
	            String maxSafeMoneyStr = nf.format(maxSafeMoney);
	           // System.out.println(maxSafeMoney+"   "+maxSafeMoneyStr);
	            maxSafeMoney = Double.valueOf(maxSafeMoneyStr);
	            double result = Math.random()*(maxSafeMoney-minMoney)+minMoney;
	            each.add(result);
	            totalMoney = totalMoney - result;
	        }
	        each.add(totalMoney);
	        return each;
	    }
	
	public static void main(String[] args) {
		List<Double> prices = divide(100d, 20, 0.01d);
		for(Double price:prices) {
			System.out.println(price);
		}
		  
	}

}
